package org.dreamwork.floatingball.service;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import org.dreamwork.floatingball.R;
import org.dreamwork.floatingball.activity.MainActivity;
import org.dreamwork.floatingball.util.ViewManager;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FloatingBallService2 extends Service {
    public static boolean D = true;
    private ExecutorService pool;
    private ViewManager manager;
    public FloatingBallService2 () {
    }

    @Override
    public IBinder onBind (Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException ("Not yet implemented");
    }

    @Override
    public void onCreate () {
        super.onCreate ();
        manager = ViewManager.getInstance (this, pool = Executors.newCachedThreadPool ());
        manager.showBall ();

        Intent intent = new Intent (this, MainActivity.class);
        intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
        PendingIntent pi = PendingIntent.getActivity (this, 2, intent, 0);
        Notification notification = new Notification.Builder (this)
                .setContentTitle (getString (R.string.app_name))
                .setSmallIcon (R.drawable.floating_ball)
                .setContentIntent (pi)
                .build ();
        startForeground (1, notification);
    }

    @Override
    public void onDestroy () {
        if (pool != null) {
            pool.shutdownNow ();
        }
        if (manager != null) {
            manager.clear ();
        }
        super.onDestroy ();
    }
}
